package com.example.controller;

import com.example.utils.Result;
import com.example.pojo.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(value = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;
     //查询所有
    @GetMapping("/users")
    public Result list(){
        log.info("查询所有用户数据");
        List<User> userList = userService.list();
        return Result.success(userList);
    }
    //删除
    @DeleteMapping("/users/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("成功删除部门：{}"+ id);
        userService.delete(id);
        return Result.success();
    }
    //通过id查询
    @GetMapping("/users/{id}")
    public Result User(@PathVariable Integer id){
        log.info("查询部门:{}"+ id);
        User user = userService.query(id);
        return Result.success(user);
    }
    //通过姓名查询
    @GetMapping("/users/router")
    public Result User(@RequestParam("loginName") String loginName){
        log.info("查询部门:{}"+ loginName);
        User user = userService.query(loginName);
        return Result.success(user);
    }
    //添加
    @PostMapping("/users")
    public Result add(@RequestBody User user){
        log.info("成功添加部门：{}"+ user);
        userService.add(user);
        return Result.success();

    }
    //修改
    @PutMapping("/users")
    public Result update(@RequestBody User user){
        log.info("成功修改{}部门"+ user);
        userService.update(user);
        return Result.success();
    }

}
